package com.wwg.threads.threads_interactive.lock_support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;

/**
 * @类描述:
 * @Author wwg
 * @date: 2021/7/28 18:28
 * @Version: 1.0
 **/
public class LockSupportTest {

    private static final Logger log = LoggerFactory.getLogger(LockSupportTest.class);


    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<>();
        Thread t1 = new Thread(() -> {

                    log.debug("size = {}", list.size());
                    if (list.size() != 5) {
                            LockSupport.park();
                    }
                    log.debug("开始执行任务!");
        });

        Thread t2 = new Thread(() -> {

                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(100);
                        list.add("abc");
                        log.debug("第{}次循环", i + 1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (list.size() == 5) {
                        LockSupport.unpark(t1);
                    }
                }
        });

        t1.start();
        Thread.sleep(1000);
        t2.start();

    }
}
